<template>
  <div class="tree-tools">
    <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
      <el-col>
        <span>{{ tree.name }}</span>
      </el-col>
      <el-col :span="4">
        <el-row type="flex" justify="end">
          <!-- 两个内容 -->
          <el-col>{{ tree.manager }}</el-col>
          <el-col>
            <!-- 下拉菜单 element -->
            <el-dropdown @command="operateDepts">
              <span>
                操作<i class="el-icon-arrow-down" />
              </span>
              <!-- 下拉菜单 -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <!-- 编辑部门和删除部门只会在子节点上显示 -->
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { delDepartments } from '@/api/departemt'
export default {
  props: {
    tree: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      ondialogVisible: false
    }
  },
  computed: {
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    operateDepts(type) {
      if (type === 'add') {
        // 添加子部门的操作
        this.$emit('on-add', this.tree)
      } else if (type === 'edit') {
        //  编辑部门的操作
      } else {
        //  删除操作
        this.$confirm('确定要删除该部门吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartments(this.tree.id)
          this.$message.success('删除成功')
          this.$emit('on-delete')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.tree-tools{
  width: 100%;
}
</style>
